<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>添加诗人</title>
</head>
<body>
	<form action="">
		姓名:<input type="text" name="name" ><br>
		年龄:<input type="number" name="age" ><br>
		国籍:<input type="text" name="guoji" ><br>
		特长:<input type="text" name="techang" ><br>
		<!-- <input type="submit" value="确定"> -->
		<input type="button" onclick="submitAdd()" value="确定添加">

	</form>

	<script>
		function submitAdd(e) {
			if(!document.querySelector('body > form > input[type="text"]:nth-child(1)').value){
				location.href="/wenxue/shirens";
				return;
			}

			var url="/wenxue/shirenupAddInfo";
			var xhr=new XMLHttpRequest();
			xhr.open('POST',url);
			xhr.onload=function (e) {
				if(xhr.response!="success"){
					alert("用户不存在！");
				}
				console.log(xhr.response);
				//跳转到表格页面
				location.href="/wenxue/shirens";
			}
			xhr.setRequestHeader("Content-Type"  
        , "application/x-www-form-urlencoded");  
			xhr.send(serilazed(document.forms[0]));
		}

		function serilazed(form) {
			//过滤出来要序列化的表单项
			var aaa=Array.prototype.slice.apply(form.children)
			aaa=aaa.filter(function (item) {
				
				return item.tagName=='INPUT' && item.name;
			}).map(function (item) {
				return item.name+"="+item.value;
			});
			return aaa.join('&');
		}
		
	</script>
</body>
</html>